
import 'dart:ui';
// import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/widgets/state_layout.dart';
///带分割线的ListView
/// 封装下拉刷新与加载更多
class DeerSeparatedListView extends StatefulWidget {

  const DeerSeparatedListView({
    Key? key,
    required this.itemCount,
    required this.itemBuilder,
    required this.onRefresh,
    this.loadMore,
    this.hasMore  = false,
    this.stateType  = StateType.empty,
    this.pageSize  = 10,
    this.padding,
    this.itemExtent = 0,
    this.separatorBuilder
  }): super(key: key);

  final RefreshCallback onRefresh;
  final LoadMoreCallback? loadMore;
  final int itemCount;
  final bool hasMore;
  final IndexedWidgetBuilder itemBuilder;
  final StateType stateType;
  /// 一页的数量，默认为10
  final int pageSize;
  final EdgeInsetsGeometry? padding;
  final double itemExtent;
  final Widget Function(BuildContext, int)? separatorBuilder;

  @override
  _DeerSeparatedListViewState createState() => _DeerSeparatedListViewState();
}

typedef RefreshCallback = Future<void> Function();
typedef LoadMoreCallback = Future<void> Function();

class _DeerSeparatedListViewState extends State<DeerSeparatedListView> {

  /// 是否正在加载数据
  bool _isLoading = false;
  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: NotificationListener(
        onNotification: (ScrollNotification note){
          /// 确保是垂直方向滚动，且滑动至底部
          if (note.metrics.pixels == note.metrics.maxScrollExtent && note.metrics.axis == Axis.vertical){
            _loadMore();
          }
          return true;
        },
        child: RefreshIndicator(
          onRefresh: widget.onRefresh,
          child:ListView.separated(// widget.itemCount == 0 ? StateLayout(type: widget.stateType) : 
            itemCount: widget.loadMore == null ? widget.itemCount : widget.itemCount + 1,
            padding: widget.padding,
            // itemExtent: widget.itemExtent,
            itemBuilder: (BuildContext context, int index){
              if (widget.itemCount == 0) {
                return Container(height: ScreenUtil.getScreenH(context), child: Center(child: Text('暂无数据', style: TextStyle(color: Colours.text_gray)),),);
              }
              /// 不需要加载更多则不需要添加FootView
              if (widget.loadMore == null){
                return widget.itemBuilder(context, index);
              }else{
                return index < widget.itemCount ? widget.itemBuilder(context, index) : MoreWidget(widget.itemCount, widget.hasMore, widget.pageSize);
              }
            },
            separatorBuilder: (context,index){
              if(widget.separatorBuilder!=null){
                return widget.separatorBuilder!(context,index);
              }else{
                return Container();
              }
            },
          )
        ),
      ),
    );
  }

  Future _loadMore() async {
    if (widget.loadMore == null){
      return;
    }
    if (_isLoading) {
      return;
    }
    if (!widget.hasMore){
      return;
    }
    _isLoading = true;
    if(widget.loadMore!=null){
      await widget.loadMore!();
    }
    _isLoading = false;
  }

}

class MoreWidget extends StatelessWidget {
  
  const MoreWidget(this.itemCount, this.hasMore, this.pageSize);

  final int itemCount;
  final bool hasMore;
  final int pageSize;
  
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          hasMore ? const CupertinoActivityIndicator() : Gaps.empty,
          hasMore ? Gaps.hGap5 : Gaps.empty,
          /// 只有一页的时候，就不显示FooterView了
          Text(hasMore ? '正在加载中...' : (itemCount < pageSize ? '' : '没有了呦~'), style: const TextStyle(color: const Color(0x8A000000))),
        ],
      ),
    );
  }
}

